package com.zr.sparesubclass.service.impl;

import com.zr.sparesubclass.mapper.SpareSubMapper;
import com.zr.sparesubclass.model.*;
import com.zr.sparesubclass.service.SpareSubService;
import com.zr.util.ResultVO;
import com.zr.util.AllRecords;
import com.zr.util.ResultVOBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/12/29.
 */
@Service
public class SpareSubServiceImpl implements SpareSubService{

    @Autowired
    private SpareSubMapper spareSubMapper;
    @Override
    public ResultVO<AllRecords> queryPage(SpareSubClass spareSubClass) {

        int count = spareSubMapper.queryCount(spareSubClass);
        List<SpareSubClass> spareSubClasses = spareSubMapper.queryPage(spareSubClass);

        //        给数据中的数据赋中文值
        assiment(spareSubClasses);
        AllRecords allRecords = new AllRecords();
        allRecords.setDataList(spareSubClasses);
        allRecords.setPageIndex(spareSubClass.getPageIndex());
        allRecords.setPageSize(spareSubClass.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);

        return ResultVOBuilder.success(allRecords);
    }



    @Override
    public ResultVO<SpareSubClassAddVo> spareSubClassAdd(SpareSubClassAddVo spareSubClassAddVo) {
        Date nowData = new Date();
        SpareSubClass spareSubClass = new SpareSubClass();
        BeanUtils.copyProperties(spareSubClassAddVo,spareSubClass);
        spareSubClass.setCreateId(1024);
        spareSubClass.setCreateName("于江");
        spareSubClass.setCreateTime(nowData);
        spareSubClass.setUpdateId(1024);
        spareSubClass.setUpdateName("于江");
        spareSubClass.setUpdateTime(nowData);
        //1.验证备件小类名称是否重复//2.验证备件小类编码是否重复
        ResultVO<SpareSubClassAddVo> validata = validata(spareSubClassAddVo);
        if (!validata.isSuccess()){
            return validata;
        }
        spareSubMapper.spareSubClassAdd(spareSubClass);
        return ResultVOBuilder.success(spareSubClassAddVo);
    }

    private ResultVO<SpareSubClassAddVo> validata(SpareSubClassAddVo spareSubClassAddVo) {
        //1.验证备件小类名称是否重复
        List<SpareSubClass> spareSubClasseName = spareSubMapper.queryByName(spareSubClassAddVo.getName());
        if (!CollectionUtils.isEmpty(spareSubClasseName)){
            return ResultVOBuilder.error("500","备件小类名称已经存在");
        }
        //2.验证备件小类编码是否重复
        List<SpareSubClass> spareSubClasseCode = spareSubMapper.queryByCode(spareSubClassAddVo.getCode());
        if (!CollectionUtils.isEmpty(spareSubClasseCode)){
            return ResultVOBuilder.error("500","备件小类编码已经存在");
        }
        return ResultVOBuilder.success(spareSubClassAddVo);
    }


    @Override
    public ResultVO<SpareSubClassUpdateVo> spareSubClassupdate(SpareSubClassUpdateVo spareSubClassUpdateVo) {
        SpareSubClass subClasses = spareSubMapper.queryById(spareSubClassUpdateVo.getId());
        if (subClasses==null){
            return ResultVOBuilder.error("500","修改对象不存在！");
        }

        Date nowData = new Date();
        SpareSubClass spareSubClass = new SpareSubClass();
        BeanUtils.copyProperties(spareSubClassUpdateVo,spareSubClass);

        spareSubClass.setUpdateId(1024);
        spareSubClass.setUpdateName("于江");
        spareSubClass.setUpdateTime(nowData);
        //1.验证备件小类名称是否重复
        List<SpareSubClass> spareSubClasseName = spareSubMapper.queryByName(spareSubClassUpdateVo.getName());
        //验证查询出来的名称是否大于1，如果大于1肯定已经存在备件编码
        if (spareSubClasseName.size()>1){
            return ResultVOBuilder.error("500","备件小类编码已经存在！");
            //验证如果查询出来的数据只有一条，那么查询出来的名称id，和要修改的这条数据的id是否一致，如果一致代表通过，如果不一致提示用户已经存在
        }else if (spareSubClasseName.size()==1){
            if (subClasses.getId()!=spareSubClasseName.get(0).getId()){
                return ResultVOBuilder.error("500","备件小类编码已经存在！");
            }
        }


        int update = spareSubMapper.spareSubClassupdate(spareSubClass);
        if (update==0){
            return ResultVOBuilder.error("500","请刷新后重试！");
        }
        return ResultVOBuilder.success(spareSubClassUpdateVo);
    }



    @Override
    public ResultVO<SpareSubClassUpdateStatusVo> updateStatus(@Valid SpareSubClassUpdateStatusVo updateStatusVo) {
        SpareSubClass subClasses = spareSubMapper.queryById(updateStatusVo.getId());
        if (subClasses==null){
            return ResultVOBuilder.error("500","修改对象不存在！");
        }

        Date nowData = new Date();
        SpareSubClass spareSubClass = new SpareSubClass();
        spareSubClass.setUpdateId(1024);
        spareSubClass.setUpdateName("于江");
        spareSubClass.setUpdateTime(nowData);

        spareSubMapper.updateStatus(spareSubClass);
        return ResultVOBuilder.success(updateStatusVo);
    }


    private void assiment(List<SpareSubClass> spareSubClasses) {

        for (SpareSubClass spareSubClass:spareSubClasses){

            if(spareSubClass.getStatus()){
                spareSubClass.setStatusName(StatusNameEnum.QIYONG.getStatus());
            }else {
                spareSubClass.setStatusName(StatusNameEnum.JINYONG.getStatus());
            }
        }

    }


}
